﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommonHelpers;
using BLL;
using PagedList;
using DLL;

namespace QuanLyBanLe
{
    public partial class FormBillHistory : Form
    {
        private BillService m_BillService;
        private List<pSearchBill_Result> m_Bills;
        private int m_PageNumber;
        private int m_PageSize;
        private int m_MaxPage;
        private int m_TotalRecord;
        private bool m_EnabledEvent;
        private int m_TotalPrice;

        public FormBillHistory()
        {
            InitializeComponent();

            m_EnabledEvent = false;
            m_PageSize = 20;
            m_PageNumber = 1;
            m_TotalPrice = 0;
            m_BillService = new BillService();
            m_Bills = new List<pSearchBill_Result>();
        }

        private void FormBillHistory_Load(object sender, EventArgs e)
        {
            dtgBill.AutoGenerateColumns = false;
            BindDataSource();
            this.Text += FormMain.GetShopInfo();
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            m_PageNumber = 1;
            BindDataSource();            
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            m_PageNumber = 1;
            ChangePage();
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            m_PageNumber = m_PageNumber > 1 ? m_PageNumber - 1 : m_PageNumber;
            ChangePage();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            m_PageNumber = m_PageNumber < m_MaxPage ? m_PageNumber + 1 : m_PageNumber;
            ChangePage();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            m_PageNumber = m_MaxPage;
            ChangePage();
        }

        private void mnrPageIndex_ValueChanged(object sender, EventArgs e)
        {
            if (m_EnabledEvent)
            {
                m_PageNumber = (int)mnrPageIndex.Value;
                ChangePage();
            }
        }

        private void GetData()
        {
            if (m_PageNumber < 1)
            {
                m_PageNumber = 1;
            }
            DateTime fromDate = dtpFromDate.Value;
            DateTime toDate = dtpToDate.Value;
            int? billId = null;
            if (!string.IsNullOrEmpty(txtBillId.Text))
            {
                billId = Convert.ToInt32(txtBillId.Text);
            }
            int? billIndex = null;
            if (!string.IsNullOrEmpty(txtBillIndex.Text))
            {
                billIndex = Convert.ToInt32(txtBillIndex.Text);
            }
            bool isPrinted = !this.cbNotPrint.Checked;
            string customerName = string.IsNullOrEmpty(txtCustomerName.Text) ? null : txtCustomerName.Text;
            int? customerPhone = null;
            if (!string.IsNullOrEmpty(txtCustomerPhone.Text))
            {
                customerPhone = Convert.ToInt32(txtCustomerPhone.Text);
            }
            string productName = string.IsNullOrEmpty(txtProductName.Text) ? null : txtProductName.Text;
            m_Bills = m_BillService.SearchBill(fromDate, toDate, billId, billIndex, isPrinted,
                customerName, customerPhone, productName, out m_TotalPrice);
        }

        private void BindDataSource()
        {
            m_EnabledEvent = false;
            GetData();
            IPagedList<pSearchBill_Result> pagedList = m_Bills.ToPagedList(m_PageNumber, m_PageSize);
            dtgBill.DataSource = pagedList.ToList();
            m_TotalRecord = pagedList.TotalItemCount;
            m_MaxPage = pagedList.PageCount;
            lblTotalBill.Text = m_TotalRecord.ToString();
            lblTotalPrice.Text = m_TotalPrice.ToMoney();
            if (m_TotalRecord > 0)
            {
                lblTotalPage.Text = "/" + m_MaxPage.ToString();
                m_EnabledEvent = false;
                mnrPageIndex.Minimum = 1;
                mnrPageIndex.Enabled = true;
                mnrPageIndex.Maximum = m_MaxPage;
                mnrPageIndex.Value = m_PageNumber;
                SetEnabledForPagingNavigator();
            }
            else
            {
                m_PageNumber = 0;
                mnrPageIndex.Minimum = 0;
                mnrPageIndex.Value = 0;
                mnrPageIndex.Enabled = false;
                btnFirst.Enabled = false;
                btnPrev.Enabled = false;
                btnLast.Enabled = false;
                btnNext.Enabled = false;
            }
            m_EnabledEvent = true;
        }

        private void SetEnabledForPagingNavigator()
        {
            if (m_PageNumber == m_MaxPage)
            {
                if (m_PageNumber == 1)
                {
                    btnFirst.Enabled = false;
                    btnPrev.Enabled = false;
                    btnLast.Enabled = false;
                    btnNext.Enabled = false;
                }
                else
                {
                    btnFirst.Enabled = true;
                    btnPrev.Enabled = true;
                    btnLast.Enabled = false;
                    btnNext.Enabled = false;
                }
            }
            else if (m_PageNumber == 1)
            {
                btnFirst.Enabled = false;
                btnPrev.Enabled = false;
                btnLast.Enabled = true;
                btnNext.Enabled = true;
            }
            else
            {
                btnFirst.Enabled = true;
                btnPrev.Enabled = true;
                btnLast.Enabled = true;
                btnNext.Enabled = true;
            }
        }

        private void ChangePage()
        {
            IPagedList<pSearchBill_Result> pagedList = m_Bills.ToPagedList(m_PageNumber, m_PageSize);
            dtgBill.DataSource = pagedList.ToList();

            // Prevent changePage call second time from mnrPageIndex_ValueChanged
            m_EnabledEvent = false;
            mnrPageIndex.Value = m_PageNumber;
            m_EnabledEvent = true;

            m_TotalRecord = pagedList.TotalItemCount;
            m_MaxPage = pagedList.PageCount;
            lblTotalPage.Text = "/" + m_MaxPage.ToString();

            SetEnabledForPagingNavigator();
        }

        private void dtgBill_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dtgBill.Columns["colViewDetail"].Index)
            {
                int billId = Convert.ToInt32(dtgBill.Rows[e.RowIndex].Cells["colBillId"].Value);
                FormBillHistoryDetail frm = new FormBillHistoryDetail(billId);
                frm.ShowDialog();
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            dtpFromDate.Value = DateTime.Now;
            dtpToDate.Value = DateTime.Now;
            txtBillId.Text = "";
            txtBillIndex.Text = "";
            txtCustomerName.Text = "";
            txtCustomerPhone.Text = "";
            txtProductName.Text = "";
            dtgBill.DataSource = null;
            dtgBill.Rows.Clear();
        }
    }
}
